//
//  SEGShowAuthCodeViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/13.
//

#import "SEGShowAuthCodeViewModel.h"
#import "SEGRequestManager.h"

@interface SEGShowAuthCodeViewModel ()

/// 请求Manager
@property (nonatomic, strong) SEGRequestManager *reqManager;

@end

@implementation SEGShowAuthCodeViewModel

- (instancetype)init {
    self = [super init];
    if (!self) return nil;
    
    [self seg_createSmsCodeCommand];
    [self seg_createSoundCodeCommand];
    [self seg_createValidateCommand];
    
    return self;
}
- (void)seg_createSmsCodeCommand {
    @weakify(self);
    self.smsCodeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(RACTuple *tuple) {
        RACTupleUnpack(NSString *phoneStr, NSString *imgCode, NSString *randomToken) = tuple;
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            [self.reqManager requestVerCodeForRegisterByRegister:phoneStr imgCode:imgCode randomToken:randomToken Finished:^(BOOL isSuccessed,id result,  NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    [subscriber sendNext:@""];
                    [subscriber sendCompleted];
                } else {
                    NSError *error = [NSError errorWithDomain:@"" code:codeType.integerValue userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                    [subscriber sendError:error];
                }
            }];
            
            return nil;
        }];
    }];
}
- (void)seg_createSoundCodeCommand {
    @weakify(self);
    self.soundCodeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(RACTuple *tuple) {
        RACTupleUnpack(NSString *phoneStr, NSString *imgCode, NSString *randomToken, NSString *type) = tuple;
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            [self.reqManager requestGetSoundCodeByTel:phoneStr imgCode:imgCode randomToken:randomToken Type:type Finished:^(BOOL isSuccessed,id result,  NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    [subscriber sendNext:errorMessage];
                    [subscriber sendCompleted];
                } else {
                    NSError *error = [NSError errorWithDomain:@"" code:codeType.integerValue userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                    [subscriber sendError:error];
                }
            }];
            
            return nil;
        }];
    }];
}
- (void)seg_createValidateCommand {
    @weakify(self);
    self.validateCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(RACTuple *tuple) {
        RACTupleUnpack(NSString *phoneStr, NSString *smsCode) = tuple;
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            [self.reqManager requestValidateCaptchasByTel:phoneStr smsCode:smsCode password:nil Finished:^(BOOL isSuccessed,id result,  NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    [subscriber sendNext:errorMessage];
                    [subscriber sendCompleted];
                } else {
                    NSError *error = [NSError errorWithDomain:@"" code:codeType.integerValue userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                    [subscriber sendError:error];
                }
            }];
            
            return nil;
        }];
    }];
}

#pragma mark - Getter
- (SEGRequestManager *)reqManager {
    if (!_reqManager) _reqManager = [[SEGRequestManager alloc] init];
    return _reqManager;
}

@end
